var express = require('express');
var multer = require('multer')
var fs = require("fs")
var router = express.Router();
const upload = multer({ dest: 'public/uploads/' })  //配置上传图片存放的目录

// 上传图片路由
router.post("/img", upload.array('file', 1), (req, res) => {
    console.log(req.files);
    // [
    //     {
    //       fieldname: 'file',
    //       originalname: '765038_144458285129_2.jpg',
    //       encoding: '7bit',
    //       mimetype: 'image/jpeg',
    //       destination: 'public/uploads/',
    //       filename: 'aeebaeef6dbb7f95f69cf6859b3ad85b',
    //       path: 'public\\uploads\\aeebaeef6dbb7f95f69cf6859b3ad85b',
    //       size: 638373
    //     }
    //   ]
    let file = req.files[0]
    let imgsrc = "public/uploads/" + "hg_" + file.originalname
    fs.renameSync(file.path, imgsrc)

    let fileDatas = {
        type: file.mimetype,
        size: file.size,
        name: imgsrc
    }
    res.json({
        status: 200,
        msg: "上传成功",
        data: fileDatas
    })


})

module.exports = router